{
 "cells": [
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "id": "a94c3ecd94fddc56"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "\"\"\"\n",
    "找出温度超过30度的天数\n",
    "计算平均温度\n",
    "将温度从高到低排序\n",
    "找出温度变化最大的两天\n",
    "\"\"\"\n",
    "\n",
    "t = pd.Series([28, 31, 29, 32, 30, 27, 33], index=['周一', '周二', '周三', '周四', '周五', '周六', '周日'])\n",
    "\n",
    "t[t > 30]\n",
    "t.mean()\n",
    "t.sort_values()\n",
    "t.sort_values(ascending=False)\n",
    "\n",
    "# 计算差值\n",
    "diff = t.diff().abs()\n",
    "diff.sort_values(ascending=False).keys().tolist()[:2]"
   ],
   "id": "e866715d69b868ed"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T08:34:39.460146Z",
     "start_time": "2025-07-28T08:34:39.451530Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "给定某股票连续10个交易日的收盘价Series:\n",
    "计算每日收益率(当日收盘价/前日收盘价-1)\n",
    "找出收益率最高和最低的日期\n",
    "计算波动率(收益率的标准差)\n",
    "\"\"\"\n",
    "\n",
    "price = pd.Series([102.3, 103.5, 105.1, 104.8, 106.2, 107.0, 106.5, 108.1, 109.3, 110.2],\n",
    "                  index=pd.date_range('2023-01-01', periods=10))\n",
    "\n",
    "print('计算每日收益率')\n",
    "p = price.pct_change()\n",
    "\n",
    "print(p.idxmax())\n",
    "print(p.idxmin())\n",
    "\n",
    "p.std()\n"
   ],
   "id": "211735e89974e937",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算每日收益率\n",
      "2023-01-03 00:00:00\n",
      "2023-01-07 00:00:00\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.007373623845361105"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T08:49:32.121792Z",
     "start_time": "2025-07-28T08:49:32.110429Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "某产品过去12个月的销售量Series:\n",
    "计算季度平均销量(每3个月为一个季度)\n",
    "找出销量最高的月份\n",
    "计算月环比增长率\n",
    "找出连续增长超过2个月的月份\n",
    "\"\"\"\n",
    "\n",
    "sales = pd.Series([120, 135, 145, 160, 155, 170, 180, 175, 190, 200, 210, 220],\n",
    "                  index=pd.date_range('2022-01-01', periods=12, freq='MS'))\n",
    "\n",
    "# 季度\n",
    "sales.resample('QS').mean()\n",
    "\n",
    "sales.idxmax()\n",
    "\n",
    "# 计算月环比增长率\n",
    "a = sales.pct_change()\n",
    "\n",
    "# 找出连续增长超过2个月的月份\n",
    "b = a > 0\n",
    "b[b.rolling(3).sum() == 3].keys().tolist()\n"
   ],
   "id": "99a039cf42ea6736",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Timestamp('2022-04-01 00:00:00'),\n",
       " Timestamp('2022-11-01 00:00:00'),\n",
       " Timestamp('2022-12-01 00:00:00')]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-28T08:57:20.386249Z",
     "start_time": "2025-07-28T08:57:20.370569Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "某商店每小时销售额Series:\n",
    "按天重采样计算每日总销售额\n",
    "计算每天营业时间(8:00-22:00)和非营业时间的销售额比例\n",
    "找出销售额最高的3个小时\n",
    "\"\"\"\n",
    "\n",
    "np.random.seed(42)\n",
    "h = pd.Series(np.random.randint(0, 100, 24),\n",
    "              index=pd.date_range('2025-01-01', periods=24, freq='h'))\n",
    "\n",
    "total = h.resample('D').sum()\n",
    "\n",
    "print(h.between_time('08:00', '22:00').sum() / total)\n",
    "\n",
    "h.nlargest(3)\n"
   ],
   "id": "491c16d932cbfe79",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-01-01    0.588382\n",
      "Freq: D, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2025-01-01 11:00:00    99\n",
       "2025-01-01 01:00:00    92\n",
       "2025-01-01 10:00:00    87\n",
       "dtype: int32"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 62
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
